namespace CommonTagTime
{
    using Microsoft.VisualBasic;
    using System;
    using System.Data;
    using System.Globalization;
    using System.Text.RegularExpressions;
    using System.Threading;
    using System.Web;
    using System.Web.Security;
    using System.Configuration;
    using System.Text;
    using System.Security.Cryptography;

    public class clsString
    {
        public clsString()
        {
        }

        public static string BriefDescription(object obj, int length)
        {
            if (obj == null)
            {
                return "";
            }
            string str = obj.ToString();
            if ((str.Length + 1) > length)
            {
                str = str.Substring(0, length + 1);
                if (str.IndexOf(" ") > -1)
                {
                    int i = str.LastIndexOf(" ");
                    str = str.Substring(0, i);
                }
                str = str + "...";
            }
            return str;
        }

        public static string AutoHyperlinks(string strvar, string param)
        {
            // First, process all <nolink> areas and change period characters temporarily to avoid auto-hyperlink processing.
            string final = strvar;

            Regex regex = new Regex(@"<nolink>(.*?)</nolink>", RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.CultureInvariant | RegexOptions.IgnorePatternWhitespace | RegexOptions.Compiled);

            MatchCollection theMatches = regex.Matches(strvar);

            for (int index = 0; index < theMatches.Count; index++)
            {
                final = final.Replace(theMatches[index].ToString(), theMatches[index].ToString().Replace(".", "[[[pk:period]]]"));
            }

            // Second, process all existing <a> tags and change period characters in them temporarily to avoid auto-hyperlink processing.
            regex = new Regex(@"<a(.*?)</a>", RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.CultureInvariant | RegexOptions.IgnorePatternWhitespace | RegexOptions.Compiled);

            theMatches = regex.Matches(final);

            for (int index = 0; index < theMatches.Count; index++)
            {
                final = final.Replace(theMatches[index].ToString(), theMatches[index].ToString().Replace(".", "[[[pk:period]]]"));
            }

            // Third, temporarily alter any digit sequences that are formatted like domain names.
            final = Regex.Replace(final, @"(?<=\d)\.(?=\d)", "[[[pk:period]]]");

            // Fourth, look for, and process, any linkable domain names or URLs.
            Regex tags = new Regex(@"([a-zA-Z0-9\:/\-]*[a-zA-Z0-9\-_]\.[a-zA-Z0-9\-_][a-zA-Z0-9\-_][a-zA-Z0-9\?\=&#_\-/\.]*[^<>,;\.\s\)\(\]\[\""])");

            // Fifth, fix any inadvertently altered protocol strings.
            final = tags.Replace(final, "<a href=\"http://$&\"" + param + ">$&</a>");
            final = final.Replace("http://https://", "https://");
            final = final.Replace("http://http://", "http://");
            final = final.Replace("http://ftp://", "ftp://");
            final = final.Replace("http://rtsp://", "rtsp://");
            final = final.Replace("http://mms://", "mms://");
            final = final.Replace("http://pcast://", "pcast://");
            final = final.Replace("http://sftp://", "sftp://");

            final = final.Replace("[[[pk:period]]]", ".");
            final = final.Replace("<nolink>", "");
            final = final.Replace("</nolink>", "");

            // Lastly, return the processed string.
            return final;
        }

        public static string BBCodeToHTML(string str, bool bBr, bool bEmotion)
        {
            str = BBCodeToHTML(str);

            // lastly, replace any new line characters with <br />
            if (bBr)
                str = str.Replace("\n", "<br />");

            string srcImg = "/Upload/Emoticon/";
            //emoticon
            str = str.Replace(":))", string.Format(@"<img src=""{0}21.gif"" />", srcImg));
            str = str.Replace(">:)", string.Format(@"<img src=""{0}19.gif"" />", srcImg));
            str = str.Replace(":((", string.Format(@"<img src=""{0}20.gif"" />", srcImg));

            str = str.Replace(":)", string.Format(@"<img src=""{0}1.gif"" />", srcImg));
            str = str.Replace(":(", string.Format(@"<img src=""{0}2.gif"" />", srcImg));
            str = str.Replace(";)", string.Format(@"<img src=""{0}3.gif"" />", srcImg));
            str = str.Replace(":D", string.Format(@"<img src=""{0}4.gif"" />", srcImg));
            str = str.Replace(":d", string.Format(@"<img src=""{0}4.gif"" />", srcImg));
            str = str.Replace(";;)", string.Format(@"<img src=""{0}5.gif"" />", srcImg));
            str = str.Replace(">:d<", string.Format(@"<img src=""{0}6.gif"" />", srcImg));
            str = str.Replace(":-/", string.Format(@"<img src=""{0}7.gif"" />", srcImg));
            str = str.Replace(":x>", string.Format(@"<img src=""{0}8.gif"" />", srcImg));
            str = str.Replace(":\"", string.Format(@"<img src=""{0}9.gif"" />", srcImg));
            str = str.Replace(":-p", string.Format(@"<img src=""{0}10.gif"" />", srcImg));
            str = str.Replace(":-P", string.Format(@"<img src=""{0}10.gif"" />", srcImg));

            str = str.Replace(":-*", string.Format(@"<img src=""{0}11.gif"" />", srcImg));
            str = str.Replace("=((", string.Format(@"<img src=""{0}12.gif"" />", srcImg));
            str = str.Replace(":-o", string.Format(@"<img src=""{0}13.gif"" />", srcImg));
            str = str.Replace(":-O", string.Format(@"<img src=""{0}13.gif"" />", srcImg));
            str = str.Replace("x-(", string.Format(@"<img src=""{0}14.gif"" />", srcImg));
            str = str.Replace(":->", string.Format(@"<img src=""{0}15.gif"" />", srcImg));
            str = str.Replace("b-)", string.Format(@"<img src=""{0}16.gif"" />", srcImg));

            str = str.Replace(":s", string.Format(@"<img src=""{0}17.gif"" />", srcImg));
            str = str.Replace(":S", string.Format(@"<img src=""{0}17.gif"" />", srcImg));
            str = str.Replace(":-s", string.Format(@"<img src=""{0}17.gif"" />", srcImg));
            str = str.Replace(":-S", string.Format(@"<img src=""{0}17.gif"" />", srcImg));
            str = str.Replace("#:-s", string.Format(@"<img src=""{0}18.gif"" />", srcImg));
            str = str.Replace("#:-S", string.Format(@"<img src=""{0}18.gif"" />", srcImg));

            str = str.Replace(":|", string.Format(@"<img src=""{0}22.gif"" />", srcImg));
            str = str.Replace("/:)", string.Format(@"<img src=""{0}23.gif"" />", srcImg));
            str = str.Replace("=))", string.Format(@"<img src=""{0}24.gif"" />", srcImg));
            str = str.Replace("o:-)", string.Format(@"<img src=""{0}25.gif"" />", srcImg));

            return str;
        }

        public static string BBCodeToHTML(string str)
        {
            Regex exp;
            str = str.Replace("\n", "[br]");
            str = str.Replace("[br]", "<br/>");
            // format the bold tags: [b][/b]
            // becomes: <strong></strong>
            exp = new Regex(@"\[b\](.+?)\[/b\]");
            str = exp.Replace(str, "<strong>$1</strong>");

            // format the italic tags: [i][/i]
            // becomes: <em></em>
            exp = new Regex(@"\[i\](.+?)\[/i\]");
            str = exp.Replace(str, "<em>$1</em>");

            // format the underline tags: [u][/u]
            // becomes: <u></u>
            exp = new Regex(@"\[u\](.+?)\[/u\]");
            str = exp.Replace(str, "<u>$1</u>");

            // format the strike tags: [s][/s]
            // becomes: <strike></strike>
            exp = new Regex(@"\[s\](.+?)\[/s\]");
            str = exp.Replace(str, "<strike>$1</strike>");

            // format the url tags: [url=www.website.com]my site[/url]
            // becomes: <a href="www.website.com">my site</a>
            exp = new Regex(@"\[url\=([^\]]+)\]([^\]]+)\[/url\]");
            str = exp.Replace(str, "<a href=\"$1\" target=\"_blank\">$2</a>");

            // format the img tags: [img]www.website.com/img/image.jpeg[/img]
            // becomes: <img src="www.website.com/img/image.jpeg" />
            exp = new Regex(@"\[img\]([^\]]+)\[/img\]");
            str = exp.Replace(str, "<img src=\"$1\" />");

            // format img tags with alt: [img=www.website.com/img/image.jpeg]this is the alt text[/img]
            // becomes: <img src="www.website.com/img/image.jpeg" alt="this is the alt text" />
            exp = new Regex(@"\[img\=([^\]]+)\]([^\]]+)\[/img\]");
            str = exp.Replace(str, "<img src=\"$1\" alt=\"$2\" />");

            //format the colour tags: [color=red][/color]
            // becomes: <font color="red"></font>
            // supports UK English and US English spelling of colour/color
            exp = new Regex(@"\[color\=([^\]]+)\]([^\]]+)\[/color\]");
            str = exp.Replace(str, "<font color=\"$1\">$2</font>");
            exp = new Regex(@"\[colour\=([^\]]+)\]([^\]]+)\[/colour\]");
            str = exp.Replace(str, "<font color=\"$1\">$2</font>");

            // format the size tags: [size=3][/size]
            // becomes: <font size="+3"></font>
            exp = new Regex(@"\[size\=([^\]]+)\]([^\]]+)\[/size\]");
            str = exp.Replace(str, "<font size=\"+$1\">$2</font>");

            return str;
        }

        public static string CutString(object obj, int length)
        {
            string str = string.Empty;
            if (obj != null)
            {
                str = obj.ToString();
                if ((str.Length + 1) > length)
                {
                    str = str.Substring(0, length + 1);
                    if (str.IndexOf(" ") > -1)
                    {
                        int i = str.LastIndexOf(" ");
                        str = str.Substring(0, i);
                    }
                    str = str + "...";
                }
            }

            return str;
        }

        public static string CheckObject(object ObjectIn, string DefaultOut)
        {
            if (ObjectIn == DBNull.Value)
            {
                return DefaultOut;
            }

            return ObjectIn.ToString();
        }

        public static string EncodePassword(string Password)
        {
            return FormsAuthentication.HashPasswordForStoringInConfigFile(Password, "MD5");
        }

        public static string Random(int size, bool lowerCase)
        {
            StringBuilder builder = new StringBuilder();
            Random random = new Random();
            char ch;
            for (int i = 0; i < size; i++)
            {
                ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
                builder.Append(ch);
            }
            if (lowerCase)
                return builder.ToString().ToLower();
            return builder.ToString();
        }

        public static string GenerateByDateTime()
        {
            string str = string.Format(@"{0:yyMMddhhmmss}{1}", DateTime.Now, DateTime.Now.Millisecond);
            return str;
        }

        public static string GenerateID50()
        {
            return Convert.ToString(Guid.NewGuid());
        }
        
        public static string QueryString(string VariableQuery)
        {
            string strQuery = "";
            if (HttpContext.Current.Request.QueryString[VariableQuery] != null && HttpContext.Current.Request.QueryString[VariableQuery] != "")
            {
                strQuery = SmoothQuote(HttpContext.Current.Request.QueryString[VariableQuery]);
            }

            return strQuery;
        }

        public static string RemoveSpace(string OldString)
        {
            string strReturn = "";
            OldString = OldString.Trim();
            for (int i = 0; i <= (OldString.Length - 1); i++)
            {
                if (OldString[i] != ' ')
                {
                    strReturn = strReturn + OldString[i];
                }
                else if ((i > 0) && (OldString[i - 1] != ' '))
                {
                    strReturn = strReturn + OldString[i];
                }
            }
            return strReturn;
        }

 
        public static string SmoothQuote(string Text)
        {
            if ((Text == null) || (Text == ""))
            {
                return "";
            }
            if (Text.IndexOf("'") > 0)
            {
                Text = Text.Replace("'", "");
            }
            return Text;
        }

        public static string GetParam(string name, string def)
        {
            string value = HttpContext.Current.Request.QueryString[name] != null ? "" + HttpContext.Current.Request.QueryString[name] : def;
            return Regex.Replace(value, @"[^0-9a-zA-Z\\-_,]+", "");
        }

        public static string MD5(string str)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] result = md5.ComputeHash(Encoding.ASCII.GetBytes(str));
            str = BitConverter.ToString(result);

            return str.Replace("-", "");
        }
        public static string ACTIVE_IMAGE_URL = "~/App_Themes/Admin/images/icons/tick.png";
        public static string DEACTIVE_IMAGE_URL = "~/App_Themes/Admin/images/icons/publish_x.png";
        public static string ORDERDOWN_IMAGE_URL = "~/App_Themes/Admin/images/icons/downarrow.png";
        public static string ORDERUP_IMAGE_URL = "~/App_Themes/Admin/images/icons/uparrow.png";
        public static string EDIT_IMAGE_URL = "~/App_Themes/Admin/images/icons/pencil.png";
        public static string DELETE_IMAGE_URL = "~/App_Themes/Admin/images/icons/cross_circle.png";
    } // class clsCommonTagTimeString
}
